/*
 * SelectedViewInterFrm.java
 *
 * Created on __DATE__, __TIME__
 */

package com.jakey.view;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.jakey.dao.SelectionDao;
import com.jakey.model.Course;
import com.jakey.model.Selection;
import com.jakey.util.DbUtil;

import Message.Message;
import client.TheClient;
import user.User;

import java.awt.Dimension;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Color;
import javax.swing.JTextPane;

/**
 *
 * @author  __USER__
 */
public class TeacherViewInterFrm extends javax.swing.JInternalFrame {

	SelectionDao selectionDao = new SelectionDao();
	private int courseId=-1;

	/** Creates new form SelectedViewInterFrm */
	public TeacherViewInterFrm() {
		//getContentPane().setBackground(new Color(64, 116, 52));
		getContentPane().setBackground(new Color(240,240,240));
		setPreferredSize(new Dimension(700, 433));
		initComponents();
		this.setLocation(200, 50);
		this.fillTable(new Course());
	}

	public TeacherViewInterFrm(User user) {
		//getContentPane().setBackground(new Color(64, 116, 52));
		getContentPane().setBackground(new Color(240,240,240));
		setPreferredSize(new Dimension(700, 433));
		initComponents();
		this.setLocation(200, 50);
		this.fillTable(new Course());
	}


	private void fillTable(Course course) {
		DefaultTableModel dtm = (DefaultTableModel) courseTable.getModel();
		dtm.setRowCount(0);
		int currentSno = 9018125;
		String tp="SELECTION_LIST";
		Message message=new Message();
		message.setCheckCode(6);
		message.setType(tp);
		message.setNumber(currentSno);
		client.TheClient client=new TheClient();
		Message received;
		try {
			received = client.sendAndReceive(message);
			Vector<Course> TbB = new Vector<Course>();
			TbB = received.getCsvecVector();
			for(int i=0;i<TbB.size();i++) {
				Course temp = new Course();
				Vector v = new Vector();
				temp = TbB.get(i);
				v.add(temp.getCourseId());
				v.add(temp.getCourseName());
				v.add(temp.getCourseTime());
				v.add(temp.getCourseTeacher());
				dtm.addRow(v);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		courseTable = new javax.swing.JTable();
		jb_selectionCancel = new javax.swing.JButton();

		setClosable(true);
		setIconifiable(true);
		setTitle("\u6559\u5E08\u8BFE\u7A0B\u67E5\u770B");

		courseTable.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "课程编号", "我的课程", "上课时间", "任课备注" }) {
			boolean[] canEdit = new boolean[] { false, false, false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		courseTable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(java.awt.event.MouseEvent evt) {
				courseTableMousePressed(evt);
			}
		});
		jScrollPane1.setViewportView(courseTable);

		jb_selectionCancel.setText("\u9000\u8BFE");
		jb_selectionCancel
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jb_selectionCancelActionPerformed(evt);
					}
				});
		
		JTextPane textPane = new JTextPane();
		textPane.setText("\u6211\u7684\u6392\u8BFE\u4FE1\u606F");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
							.addContainerGap()
							.addComponent(jScrollPane1, GroupLayout.DEFAULT_SIZE, 668, Short.MAX_VALUE))
						.addGroup(layout.createSequentialGroup()
							.addGap(306)
							.addComponent(jb_selectionCancel))
						.addGroup(layout.createSequentialGroup()
							.addContainerGap()
							.addComponent(textPane, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(textPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(10)
					.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 278, GroupLayout.PREFERRED_SIZE)
					.addGap(26)
					.addComponent(jb_selectionCancel)
					.addContainerGap(36, Short.MAX_VALUE))
		);
		getContentPane().setLayout(layout);

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void courseTableMousePressed(java.awt.event.MouseEvent evt) {
//		int row = courseTable.getSelectedRow();
//		courseId = Integer.parseInt((String) courseTable.getValueAt(row, 0));
		int count=courseTable.getSelectedRow();
		String a=courseTable.getValueAt(count, 0).toString();
		String b=courseTable.getValueAt(count, 1).toString();
		String c=courseTable.getValueAt(count, 2).toString();
		String d=courseTable.getValueAt(count, 3).toString();

		int courseId = Integer.parseInt(a);
	}

	private void jb_selectionCancelActionPerformed(java.awt.event.ActionEvent evt) {
		int count=courseTable.getSelectedRow();
		String a=courseTable.getValueAt(count, 0).toString();
		String b=courseTable.getValueAt(count, 1).toString();
		String c=courseTable.getValueAt(count, 2).toString();
		String d=courseTable.getValueAt(count, 3).toString();

		int courseId = Integer.parseInt(a);
		if (courseId==-1) {
			JOptionPane.showMessageDialog(this, "请选择一门课程！");
			return;
		}
		int n = JOptionPane.showConfirmDialog(this, "确定要退选该门课程吗?");
		if (n == 0) {
			
			int currentSno = 9018125;
			Selection selection = new Selection(courseId, currentSno);
			
				Message message = new Message();
				message.setAllembracing(selection);
				message.setType("SELECTION_CANCEL");
				message.setCheckCode(6);				
				client.TheClient client = new TheClient();

				Message recevied = new Message();
				try {
				recevied = client.sendAndReceive(message);
				if (recevied.isResponse()) {
					JOptionPane.showMessageDialog(this, "退选成功!");
					this.fillTable(new Course());
				} else {
					JOptionPane.showMessageDialog(this, "退选成功!");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
				JOptionPane.showMessageDialog(this, "退选成功!");
			} finally {
				try {

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JTable courseTable;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JButton jb_selectionCancel;
}